package com.wuxiaofan.product.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuxiaofan.product.entity.Attr;
import com.wuxiaofan.product.entity.custom.AttrCustom;
import com.wuxiaofan.product.entity.custom.AttrGroupCustom;
import com.wuxiaofan.product.service.AttrService;
import com.wuxiaofan.common.vo.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.swagger.annotations.Api;

@RestController
@Api("商品属性")
@RequestMapping("/attr")
public class AttrApi {


    @Autowired
    private AttrService pmsAttrService;

    @GetMapping("/{attrType}/list/{categoryId}/{current}/{size}")
    public ReturnResult list(@PathVariable("attrType") String attrType, @PathVariable("categoryId") String categoryId,
            @PathVariable("current") int current,
            @PathVariable("size") int size) {
        Page page = new Page(current, size);
        Page<AttrCustom> attrCustoms = pmsAttrService.findPageByAttrTypeAndCategoryId(attrType,categoryId, page);
        return ReturnResult.success("", attrCustoms);
    }


    @GetMapping("/findById/{id}")
    public ReturnResult findById(@PathVariable("id") String id) {
        Attr pmsAttr = pmsAttrService.findById(id);
        return ReturnResult.success("", pmsAttr);
    }

    @RequestMapping("findByMap")
    public ReturnResult findByMap(@RequestBody JSONObject jsonObject) {
        Map paraMap = new HashMap();
        paraMap.putAll(jsonObject);
        List<AttrCustom> pmsAttrCustoms = pmsAttrService.findByMap(paraMap);
        return ReturnResult.success("", pmsAttrCustoms);
    }

    @RequestMapping("all")
    public ReturnResult all() {
        Map paraMap = new HashMap();
        List<AttrCustom> pmsAttrCustoms = pmsAttrService.findByMap(paraMap);
        return ReturnResult.success("", pmsAttrCustoms);
    }

    @RequestMapping("findByPage")
    public ReturnResult findByPage(@RequestBody JSONObject jsonObject) {
        Page page = new Page(jsonObject.getIntValue("current"), jsonObject.getIntValue("size"));
        Map paraMap = new HashMap();
        paraMap.putAll(jsonObject);
        IPage<AttrCustom> pmsAttrCustoms = pmsAttrService.findByMap(paraMap, page);
        return ReturnResult.success("", pmsAttrCustoms);
    }

    @RequestMapping("save")
    public ReturnResult save(@RequestBody Attr pmsAttr) {
        pmsAttr = pmsAttrService.save(pmsAttr);
        return ReturnResult.success("保存成功", pmsAttr);
    }

    @RequestMapping("update")
    public ReturnResult update(@RequestBody JSONObject jsonObject) {
        Map paraMap = jsonObject;
        pmsAttrService.updateMapById(paraMap);
        return ReturnResult.success("更新成功");
    }


    @RequestMapping("delById")
    public ReturnResult delById(@RequestBody JSONObject jsonObject) {
        pmsAttrService.delById(jsonObject.getString("id"));
        return ReturnResult.success("删除成功");
    }


}
